package com.laxcen.toydemo;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;

import androidx.annotation.Nullable;

import com.laxcen.controls.HomeMenuButtonControl;
import com.laxcen.intelligenttoy.BLE.ETag;

import util.BackgroundMusic;
import util.DelayHandler;

public class VideoPlayerActivity extends BaseActivity implements View.OnClickListener {
    GameState mGameState = GameState.instance(this);
    ImageButton btn_menu_course = null;
    VideoView videoPlayer = null;
    ImageView imgVideoPoster = null;
    HomeMenuButtonControl btn_home, btn_play, btn_pasue = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        mGameState.setStep(GameState.EGameStep.VideoPlayer);

        imgVideoPoster = findViewById(R.id.imgVideoPoster);
        videoPlayer = findViewById(R.id.videoPlayer);
        btn_home = findViewById(R.id.btn_home);
        btn_home.setOnClickListener(this);
        btn_play = findViewById(R.id.btn_play);
        btn_play.setOnClickListener(this);
        btn_pasue = findViewById(R.id.btn_pasue);
        btn_pasue.setOnClickListener(this);

        MediaController localMediaController = new MediaController(this);
        videoPlayer.setMediaController(localMediaController);
        String uri = ("android.resource://" + getPackageName() + "/" + R.raw.vido01);
        videoPlayer.setVideoURI(Uri.parse(uri));

        startPlayer();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_home) {
            startActivityAndFinish(LearningActivity.class);
        } else if (v.getId() == R.id.btn_play) {
            startPlayer();
        } else if (v.getId() == R.id.btn_pasue) {
            videoPlayer.pause();
        }
    }

    private void startPlayer() {
        imgVideoPoster.setVisibility(View.GONE);
        videoPlayer.start();
    }


    @Override
    protected void onResume() {
        super.onResume();
        restartTagCheck();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    private boolean isWaitTag = true;

    private void restartTagCheck() {
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                isWaitTag = true;
                startTagCheck();
            }
        }, 1000);
    }

    /* 7,8,9 = 1,2,3 */
    private void startTagCheck() {
        if (!isWaitTag) return;
        if (checkOption(currentTags, ETag._GOBACK)) {
            isWaitTag = false;
            startActivityAndFinish(LearningActivity.class);
        }

        if (isWaitTag) {
            DelayHandler.newHandler().delay(new Runnable() {
                @Override
                public void run() {
                    startTagCheck();
                }
            }, LoopDetectionMillis);
        }
    }
}
